package com.xcw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xcw.dao.mapper.UsersMapper;
import com.xcw.dao.model.Users;
import com.xcw.service.UserService;
import com.xcw.utils.MD5Utils;
import com.xcw.vo.ResStatus;
import com.xcw.vo.ResultVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UsersMapper usersMapper;

    @Override
    public ResultVO checkLogin(String name, String pwd) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",name);
        List<Users> usersList = usersMapper.selectList(queryWrapper);
        if (usersList.size() == 0){
            return new ResultVO(ResStatus.NO,"登录失败，用户名不存在！",null);
        } else{
            String md5Pwd = MD5Utils.md5(pwd);
            if (md5Pwd.equals(usersList.get(0).getPassword())){
                return new ResultVO(ResStatus.OK, "登陆成功", usersList.get(0));
            } else{
                return new ResultVO(ResStatus.NO, "密码错误，请重试！", null);
            }
        }
    }
}
